﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using YuuzleComic.IDAL;
using YuuzleComic.DBUtility;//Please add references
namespace YuuzleComic.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:zj
    /// </summary>
    public partial class zj : Izj
    {
        public zj()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("id", "zj");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from zj");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该章节名称
        /// </summary>
        public bool Exists(string zjmc, int comicid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from zj");
            strSql.Append(" where zjmc=@zjmc and cid=@comicid");
            SqlParameter[] parameters = {
					new SqlParameter("@zjmc", SqlDbType.VarChar),
                    new SqlParameter("@comicid", SqlDbType.Int)};
            parameters[0].Value = zjmc;
            parameters[1].Value = comicid;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该章节漫画地址
        /// </summary>
        public bool ExistsAdd(string tpadd, int comicid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from zj");
            strSql.Append(" where tpadd=@tpadd and cid=@comicid");
            SqlParameter[] parameters = {
					new SqlParameter("@tpadd", SqlDbType.VarChar),
                    new SqlParameter("@comicid", SqlDbType.Int)};
            parameters[0].Value = tpadd;
            parameters[1].Value = comicid;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(YuuzleComic.Model.zj model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into zj(");
            strSql.Append("cid,zjmc,tpadd,tps,tpf,time,tpurl,hits,cjitemid,isdownload)");
            strSql.Append(" values (");
            strSql.Append("@cid,@zjmc,@tpadd,@tps,@tpf,@time,@tpurl,@hits,@cjitemid,@isdownload)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@cid", SqlDbType.Int,4),
					new SqlParameter("@zjmc", SqlDbType.VarChar,50),
					new SqlParameter("@tpadd", SqlDbType.VarChar,255),
					new SqlParameter("@tps", SqlDbType.Int,4),
					new SqlParameter("@tpf", SqlDbType.Int,4),
					new SqlParameter("@time", SqlDbType.DateTime,4),
					new SqlParameter("@tpurl", SqlDbType.Text),
					new SqlParameter("@hits", SqlDbType.Int,4),
					new SqlParameter("@cjitemid", SqlDbType.Int,4),
                    new SqlParameter("@isdownload", SqlDbType.Bit)};
            parameters[0].Value = model.cid;
            parameters[1].Value = model.zjmc;
            parameters[2].Value = model.tpadd;
            parameters[3].Value = model.tps;
            parameters[4].Value = model.tpf;
            parameters[5].Value = model.time;
            parameters[6].Value = model.tpurl;
            parameters[7].Value = model.hits;
            parameters[8].Value = model.cjitemid;
            parameters[9].Value = model.isdownload;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                int id = Convert.ToInt32(obj);
                DbHelperSQL.ExecuteSql("update zj set ordernum=" + id + " where id=" + id);
                return id;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(YuuzleComic.Model.zj model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update zj set ");
            strSql.Append("cid=@cid,");
            strSql.Append("zjmc=@zjmc,");
            strSql.Append("tpadd=@tpadd,");
            strSql.Append("tps=@tps,");
            strSql.Append("tpf=@tpf,");
            strSql.Append("time=@time,");
            strSql.Append("tpurl=@tpurl,");
            strSql.Append("cjitemid=@cjitemid,");
            strSql.Append("isdownload=@isdownload");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@cid", SqlDbType.Int,4),
					new SqlParameter("@zjmc", SqlDbType.VarChar,50),
					new SqlParameter("@tpadd", SqlDbType.VarChar,255),
					new SqlParameter("@tps", SqlDbType.Int,4),
					new SqlParameter("@tpf", SqlDbType.Int,4),
                    new SqlParameter("@time", SqlDbType.DateTime,4),
					new SqlParameter("@tpurl", SqlDbType.Text),
                    new SqlParameter("@cjitemid", SqlDbType.Int,4),
                    new SqlParameter("@isdownload", SqlDbType.Bit),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.cid;
            parameters[1].Value = model.zjmc;
            parameters[2].Value = model.tpadd;
            parameters[3].Value = model.tps;
            parameters[4].Value = model.tpf;
            parameters[5].Value = model.time;
            parameters[6].Value = model.tpurl;
            parameters[7].Value = model.cjitemid;
            parameters[8].Value = model.isdownload;
            parameters[9].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from zj ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from zj ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public YuuzleComic.Model.zj GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,cid,zjmc,tpadd,tps,tpf,adddate,time,tpurl,hits,cjitemid,isdownload from zj ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            YuuzleComic.Model.zj model = new YuuzleComic.Model.zj();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["cid"].ToString() != "")
                {
                    model.cid = int.Parse(ds.Tables[0].Rows[0]["cid"].ToString());
                }
                model.zjmc = ds.Tables[0].Rows[0]["zjmc"].ToString();
                model.tpadd = ds.Tables[0].Rows[0]["tpadd"].ToString();
                if (ds.Tables[0].Rows[0]["tps"].ToString() != "")
                {
                    model.tps = int.Parse(ds.Tables[0].Rows[0]["tps"].ToString());
                }
                if (ds.Tables[0].Rows[0]["tpf"].ToString() != "")
                {
                    model.tpf = int.Parse(ds.Tables[0].Rows[0]["tpf"].ToString());
                }
                if (ds.Tables[0].Rows[0]["adddate"].ToString() != "")
                {
                    model.adddate = int.Parse(ds.Tables[0].Rows[0]["adddate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["time"].ToString() != "")
                {
                    model.time = Convert.ToDateTime(ds.Tables[0].Rows[0]["time"]);
                }
                model.tpurl = ds.Tables[0].Rows[0]["tpurl"].ToString();
                if (ds.Tables[0].Rows[0]["hits"].ToString() != "")
                {
                    model.hits = int.Parse(ds.Tables[0].Rows[0]["hits"].ToString());
                }
                if (ds.Tables[0].Rows[0]["cjitemid"].ToString() != "")
                {
                    model.cjitemid = int.Parse(ds.Tables[0].Rows[0]["cjitemid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["isdownload"].ToString() != "")
                {
                    model.isdownload = Convert.ToBoolean(ds.Tables[0].Rows[0]["isdownload"]);
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,cid,zjmc,tpadd,tps,tpf,adddate,tpurl,hits,cjitemid,time,isdownload,ordernum ");
            strSql.Append(" FROM zj ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,cid,zjmc,tpadd,tps,tpf,adddate,tpurl,hits,cjitemid ");
            strSql.Append(" FROM zj ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
            parameters[0].Value = "zj";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 1;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage", parameters, "ds");
        }

        #endregion  Method

        /// <summary>
        /// 章节下载完成
        /// </summary>
        /// <param name="zjid"></param>
        public void DownloadComplete(int zjid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update zj set ");
            strSql.Append("isdownload=@isdownload");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@isdownload", SqlDbType.Bit),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = 1;
            parameters[1].Value = zjid;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 获得最上的未下载
        /// </summary>
        public DataSet GetTopDownload(int top, string strwhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top(" + top + ") a.id,a.mhmc,a.mhfm,b.id zjid,b.zjmc,b.tps,b.tpadd,b.tpurl from comics a join zj b on a.id=b.cid where (b.isdownload <> 1 or b.isdownload is null)");
            if (!string.IsNullOrEmpty(strwhere))
            {
                strSql.Append(" and " + strwhere);
            }
            strSql.Append(" order by a.id desc");
            return DbHelperSQL.Query(strSql.ToString(), 999);
        }

        public int GetDownloadCount()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(id) from zj");
            strSql.Append(" where (isdownload <> 1 or isdownload is null)");
            return Convert.ToInt32(DbHelperSQL.Query(strSql.ToString()).Tables[0].Rows[0][0]);
        }
    }
}

